package com.slorgs.business.mq

import com.slorgs.infrastructure.dto.ResponseDTO
import org.apache.rocketmq.spring.core.RocketMQTemplate
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Component

@Component
class MessageProducer {

    companion object {
        private val log = LoggerFactory.getLogger(MessageProducer::class.java)
    }

    private var rocketMQTemplate: RocketMQTemplate? = null;

    @Autowired
    fun setRocketMQTemplate(rocketMQTemplate: RocketMQTemplate) {
        this.rocketMQTemplate = rocketMQTemplate
    }

    fun sendMessage(topic: String?, message: String?): ResponseDTO? {
        val responseDTO = ResponseDTO()
        try {
            rocketMQTemplate!!.convertAndSend(topic!!, message!!)
            responseDTO.code = HttpStatus.OK.value()
        } catch (e: Exception) {
            responseDTO.code = HttpStatus.BAD_REQUEST.value()
            responseDTO.message = e.message
            log.error("消息队列同步缓存发送通知异常,{}", e.message)
        }
        return responseDTO
    }

}